﻿using System;
using System.Net;
using System.Windows;
using SuperLauncher.Entitys;
using SuperLauncher.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
using SuperLauncher.Views;
using SL8.SL.Commands;
using SL8.SL;

namespace SuperLauncher.ViewModels
{
    public class JumpListItemsTreeViewModel : SL8.SL.ViewModels.ViewModelBase
    {
        public System.Collections.ObjectModel.ObservableCollection<CategoryInfo> CategoryInfos
        {
            get
            {
                if (DataHelper.CategoryInfos == null)
                {
                    DataHelper.CategoryInfos = DataHelper.ReadData();
                    JumpListHelper.RefreshJumplist(DataHelper.CategoryInfos);
                }

                return DataHelper.CategoryInfos;
            }
        }

        public ICommand NewJumpListItemCommand
        {
            get
            {
                return this.GetCacheValue(o => o.NewJumpListItemCommand, new DelegateCommand(this.OnNewJumpListItemCommand));
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return this.GetCacheValue(o => o.CancelCommand, new DelegateCommand(this.OnCancelCommand));
            }
        }

        public ICommand EditCommand
        {
            get
            {
                return this.GetCacheValue(o => o.EditCommand, new DelegateCommand(this.OnEditCommand));
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return this.GetCacheValue(o => o.DeleteCommand, new DelegateCommand(this.OnDeleteCommand));
            }
        }

        public ICommand DeleteCategoryCommand
        {
            get
            {
                return this.GetCacheValue(o => o.DeleteCategoryCommand, new DelegateCommand(this.OnDeleteCategoryCommand));
            }
        }

        private void OnDeleteCategoryCommand(object arg)
        {
            if (MessageBox.Show("Delete?", string.Empty, MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }

            CategoryInfo category = (CategoryInfo)arg;
            this.CategoryInfos.Remove(category);
        }

        private void OnEditCommand(object arg)
        {
            ItemInfo itemInfo = (ItemInfo)arg;
            string categoryText = string.Empty;
            bool isFindCategory = false;

            foreach (var category in this.CategoryInfos)
            {
                foreach (var item in category.Items)
                {
                    if (item == itemInfo)
                    {
                        categoryText = category.Title;
                        isFindCategory = true;
                        break;
                    }
                }

                if (isFindCategory)
                {
                    break;
                }
            }

            JumpListItemInfoEditView view = new JumpListItemInfoEditView();
            view.LayoutRoot.DataContext = new JumpListItemInfoEditViewModel()
            {
                EditStatus = EditStatusEnum.Edit,
                JumpListItem = itemInfo,
                CategoryTitle = categoryText
            };

            this.ShowEditPanel(view);
        }

        private void OnDeleteCommand(object arg)
        {
            if (MessageBox.Show("Delete?", string.Empty, MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }

            ItemInfo itemInfo = (ItemInfo)arg;
            CategoryInfo categoryInfo = null;
            bool isFindCategory = false;

            foreach (var category in this.CategoryInfos)
            {
                foreach (var item in category.Items)
                {
                    if (item == itemInfo)
                    {
                        categoryInfo = category;
                        isFindCategory = true;
                        break;
                    }
                }

                if (isFindCategory)
                {
                    break;
                }
            }

            if (categoryInfo != null)
            {
                categoryInfo.Items.Remove(itemInfo);
            }

            this.SaveToXml();
        }

        private void OnCancelCommand(object arg)
        {
            Application.Current.MainWindow.Close();
        }

        private void SaveToXml()
        {
            DataHelper.SaveData(this.CategoryInfos);
            JumpListHelper.RefreshJumplist(this.CategoryInfos);
            DataHelper.SetOverlayIconAccept();
        }

        private void OnNewJumpListItemCommand(object arg)
        {
            JumpListItemInfoEditView view = new JumpListItemInfoEditView();
            view.LayoutRoot.DataContext = new JumpListItemInfoEditViewModel()
            {
                EditStatus = EditStatusEnum.Add
            };

            this.ShowEditPanel(view);
        }

        private void ShowEditPanel(JumpListItemInfoEditView view)
        {
            ChildWindow childWindow = new ChildWindow();
            childWindow.Width = 350;
            childWindow.Height = 310;
            childWindow.Content = view;
            childWindow.Show();
        }
    }
}
